import axios from 'axios'
import {getBaseApi} from '../config'

axios.defaults.baseURL = getBaseApi()
axios.defaults.headers.post['Content-Type'] = 'application/json'

//requests默认配置
const requests = axios.create({
  method: 'GET',
  timeout: 4000,
})

//请求拦截器
requests.interceptors.request.use(config => {
  return config
}, err => {
  return Promise.reject(err.response.data)
  //console.log('请求拦截器：',err)
})

//响应拦截器
requests.interceptors.response.use(response => {
  // console.log(response)
  if (response.status === 204 || response.status === 200) {
    response.success = true
  } else {
    response.success = false
  }
  return response
}, err => {
  // if (err)
  console.error(err.response.request.responseURL, '-------错误响应，返回数据为：', err.response.data)
  return Promise.reject(err.response.data)
})

export { requests }